﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using log4net;
/*
 * Coder:Tieria
 * WriteTime:2015.5.13
 * FileName:LogHelper.cs
 * ReMark:日志类,写入日志的
 */
namespace FileDFS.Utils
{
    public class LogHelper
    {
        public static Queue<string> ExcMsg;
        static LogHelper()
        {
            ExcMsg = new Queue<string>();
            ThreadPool.QueueUserWorkItem(u =>
            {
                while (true)
                {
                    string str = string.Empty;
                    if (ExcMsg.Count > 0)
                    {
                        str = ExcMsg.Dequeue();
                        //往日志文件里面写就可以了。
                        if (!string.IsNullOrEmpty(str))
                        {

                            ILog log = log4net.LogManager.GetLogger("Logs");
                            log.Error(str);
                        }
                        else
                        {
                            Thread.Sleep(50);
                        }
                    }
                    else   //如果错误消息队列里面的数据小于0，则休息
                    {
                        Thread.Sleep(50); //休息下，防止cpu空转
                    }
                }
            });
        }

        public static void WriteLog(string msg)
        {
            lock (ExcMsg)
            {
                ExcMsg.Enqueue(msg);
            }
        }
    }
}
